package com.ruoyi.common.utils;

import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

public class EntityToQueryWrapperConverter {

	public static <T> QueryWrapper<T> convert(T entity) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        if (entity == null) {
            return queryWrapper;
        }
        Class<?> clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            TableField tableField = field.getAnnotation(TableField.class);
            if (tableField != null && !tableField.exist()) {
                continue;
            }
            try {
                Object value = field.get(entity);
                if (value != null) {
                    String fieldName = field.getName();
                    // 这里简单处理为相等查询，可根据需求修改为其他查询条件
                    queryWrapper.eq(fieldName, value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return queryWrapper;
    }
	
}
